/*
 * SearchResultsDialog.java
 *
 * Created on 5 mars 2007, 08:13
 */

package blbarc;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author  alexis
 */
public class SearchResultsDialog extends javax.swing.JDialog {
    
    /** Creates new form SearchResultsDialog */
    public SearchResultsDialog(java.awt.Frame parent, boolean modal, blbarc.Database db) {
        super(parent, modal);
        this.db = db;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        ShowButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jList1KeyPressed(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        ShowButton.setMnemonic(i18n.getInstance().getString("show_mnemo").charAt(0));
        ShowButton.setText(i18n.getInstance().getString("show"));
        ShowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShowButtonActionPerformed(evt);
            }
        });

        CancelButton.setMnemonic(i18n.getInstance().getString("cancel_mnemo").charAt(0));
        CancelButton.setText(i18n.getInstance().getString("cancel"));
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(CancelButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ShowButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ShowButton)
                    .add(CancelButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            this.ShowButtonActionPerformed(null);
        }
    }//GEN-LAST:event_jList1KeyPressed

    private void ShowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ShowButtonActionPerformed
        if (this.jList1.getSelectedIndex() > -1) {
            EditForm form = new EditForm(null,true,this.db);
            form.fillData(this.barcodes[this.jList1.getSelectedIndex()]);
            form.setDeleteVisible(true);
            form.setTitle(i18n.getInstance().getString("showingarticle"));
            this.setVisible(false);
            form.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, i18n.getInstance().getString("noitemselected"),
                    i18n.getInstance().getString("error"), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_ShowButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    void fillResults(Barcode[] barcode) {
        barcodes = barcode;
        tablemodel = new javax.swing.DefaultListModel();
        for (int i = 0; i < barcode.length; i++) {
            tablemodel.addElement(barcode[i].title);
        }
        this.jList1.setModel(tablemodel);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton ShowButton;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private DefaultListModel tablemodel;
    private Barcode[] barcodes = null;

    private Database db;
}
